#import "file_config_resource.h"

/*menu
*/
std::vector<std::string> g_CfgMenuMainResourceTable;
std::vector<std::string> g_CfgMenuChapterResourceTable;

/*game
*/
std::vector<std::string> g_CfgGameBaseResourceTable;
std::vector<std::string> g_CfgGameMenuResourceTable;
std::vector<std::string> g_CfgGameItemResourceTable;
std::map<int, std::vector<std::string> > g_CfgGameBackgroundResourceTable;
std::map<int, std::vector<std::string> > g_CfgGameBowResourceTable;
std::map<int, std::vector<std::string> > g_CfgGameEnemyResourceTable;

/*library
*/
std::vector<std::string> g_CfgLibraryMainResourceTable;
std::vector<std::string> g_CfgLibraryExperimentResourceTable;
std::vector<std::string> g_CfgLibraryTreasureResourceTable;

void CfgResourceLoadMapTable(std::map<int, std::vector<std::string> > *Table, mxml_node_t *Bouy);

void CfgResourceLoadConfigFile(void)
{
	char _Buffer[256];
	FILE *_ConfigFile;
	mxml_node_t *_NodeXml, *_NodeBouy;

	memset(_Buffer, 0, sizeof _Buffer);
	[[CCFileUtils fullPathFromRelativePath:@"file_config_resource.xml"] getCString:_Buffer maxLength:sizeof _Buffer encoding:NSASCIIStringEncoding];
	_ConfigFile = fopen(_Buffer, "r");
	if (NULL != _ConfigFile)
	{
		_NodeXml = mxmlLoadFile(NULL, _ConfigFile, MXML_TEXT_CALLBACK);
		_NodeBouy = mxmlGetFirstChild(_NodeXml);
		for (; NULL != _NodeBouy; _NodeBouy = mxmlWalkNext(_NodeBouy, _NodeXml, MXML_NO_DESCEND))
		{
			/*menu
			*/
			if (tmxmlIsElement(_NodeBouy, "menu_main"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgMenuMainResourceTable, "path");
			}
			else if (tmxmlIsElement(_NodeBouy, "menu_chapter"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgMenuChapterResourceTable, "path");
			}
			/*game
			*/
			else if (tmxmlIsElement(_NodeBouy, "game_base"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgGameBaseResourceTable, "path");
			}
			else if (tmxmlIsElement(_NodeBouy, "game_menu"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgGameMenuResourceTable, "path");
			}
			else if (tmxmlIsElement(_NodeBouy, "game_item"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgGameItemResourceTable, "path");
			}
			else if (tmxmlIsElement(_NodeBouy, "game_background"))
			{
				CfgResourceLoadMapTable(&g_CfgGameBackgroundResourceTable, _NodeBouy);
			}
			else if (tmxmlIsElement(_NodeBouy, "game_bow"))
			{
				CfgResourceLoadMapTable(&g_CfgGameBowResourceTable, _NodeBouy);
			}
			else if (tmxmlIsElement(_NodeBouy, "game_enemy"))
			{
				CfgResourceLoadMapTable(&g_CfgGameEnemyResourceTable, _NodeBouy);
			}
			/*library
			*/
			else if (tmxmlIsElement(_NodeBouy, "library_main"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgLibraryMainResourceTable, "path");
			}
			else if (tmxmlIsElement(_NodeBouy, "library_experiment"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgLibraryExperimentResourceTable, "path");
			}
			else if (tmxmlIsElement(_NodeBouy, "library_treasure"))
			{
				tmxmlReadStringTable(_NodeBouy, &g_CfgLibraryTreasureResourceTable, "path");
			}
		}

		mxmlDelete(_NodeXml);
		fclose(_ConfigFile);
	}
	else
	{
		exit(0);
	}
}

void CfgResourceLoadMapTable(std::map<int, std::vector<std::string> > *Table, mxml_node_t *Bouy)
{
	mxml_node_t *_NodeElement, *_NodeElementBouy;
	int _Key;
	std::vector<std::string> _Data;

	_NodeElement = mxmlGetFirstChild(Bouy);
	for (; NULL != _NodeElement; _NodeElement = mxmlWalkNext(_NodeElement, Bouy, MXML_NO_DESCEND))
	{
		if (tmxmlIsElement(_NodeElement, "element"))
		{
			_Key = -1;
			_Data.clear();
			_NodeElementBouy = mxmlGetFirstChild(_NodeElement);
			for (; NULL != _NodeElementBouy; _NodeElementBouy = mxmlWalkNext(_NodeElementBouy, _NodeElement, MXML_NO_DESCEND))
			{
				if (tmxmlIsElement(_NodeElementBouy, "key"))
				{
					_Key = tmxmlElementReadChildInt(_NodeElementBouy);
				}
				else if (tmxmlIsElement(_NodeElementBouy, "data"))
				{
					tmxmlReadStringTable(_NodeElementBouy, &_Data, "path");
				}
			}
			Table->insert(std::pair<int, std::vector<std::string> >(_Key, _Data));
		}
	}
}